package com.welink.cmp.meatdata.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.base.statemachine.StateMachine;
import com.welink.base.statemachine.StateMachineFactory;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.dataobject.RouterDO;
import com.welink.cmp.domain.metadata.enums.ResourceTypeEnum;
import com.welink.cmp.dto.metadata.MetadataIdListQry;
import com.welink.cmp.dto.metadata.router.*;
import com.welink.cmp.dto.metadata.router.clientobject.RouterCO;
import com.welink.cmp.dto.metadata.router.query.RouterListByPageQry;
import com.welink.cmp.meatdata.command.RouterCmdExe;
import com.welink.cmp.statemachine.StateMachineIdContent;
import com.welink.cmp.statemachine.context.CommonCtx;
import com.welink.cmp.statemachine.machine.CommonEvent;
import com.welink.cmp.statemachine.machine.CommonState;
import com.welink.common.dto.PageResponse;
import com.welink.common.dto.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Router应用服务类
 * 提供Router相关的应用服务接口
 */
@Service
public class RouterAppService {

    @Autowired
    private RouterCmdExe routerCmdExe;

    /**
     * 添加Router
     *
     * @param addCmd 添加Router的命令对象
     * @return 新增Router的ID
     */
    public Long addRouter(CreateRouterAddCmd addCmd) {
        return routerCmdExe.save(addCmd);
    }

    /**
     * 配置路由器基础信息
     *
     * @param cmd 配置路由器基础信息指令
     * @return 操作结果
     */
    public Boolean setupRouterBaseInfo( SetupRouterBaseUpdateCmd cmd) {
        RouterDO routerDO = new RouterDO();
        if (cmd.isNeedsOperator()){
            routerDO.setUpdateBy(cmd.getOperator());
        }
        BeanUtils.copyProperties(cmd,routerDO);
        return routerCmdExe.update(routerDO);
    }

    /**
     * 配置路由器网络信息
     *
     * @param cmd 配置路由器网络信息指令
     * @return 操作结果
     */
    public Boolean setupRouterNetInfo(SetupRouterNetUpdateCmd cmd) {
        RouterDO routerDO = new RouterDO();
        if (cmd.isNeedsOperator()){
            routerDO.setUpdateBy(cmd.getOperator());
        }
        BeanUtils.copyProperties(cmd,routerDO);
        return routerCmdExe.update(routerDO);
    }

    /**
     * 配置路由器物理机房信息
     *
     * @param cmd 配置路由器物理机房信息指令
     * @return 操作结果
     */
    public Boolean setupRouterRackInfo(SetupRouterRackUpdateCmd cmd) {
        RouterDO routerDO = new RouterDO();
        if (cmd.isNeedsOperator()){
            routerDO.setUpdateBy(cmd.getOperator());
        }
        BeanUtils.copyProperties(cmd,routerDO);
        return routerCmdExe.update(routerDO);
    }

    /**
     * 上架路由器
     * @param launchRouterCmd 上架路由器
     * @return NEXT STATE
     */
    public Boolean launchRouter(LaunchRouterUpdateCmd launchRouterCmd){
        StateMachine<CommonState, CommonEvent, CommonCtx> stateMachine = StateMachineFactory.get(StateMachineIdContent.common_state_machine_id);
        CommonCtx ctx = CommonCtx.builder()
                .id(launchRouterCmd.getId())
                .operator(launchRouterCmd.getOperator())
                .typeEnum(ResourceTypeEnum.ROUTER)
                .launchedTime(launchRouterCmd.getLaunched_time())
                .build();
        final CommonState state = stateMachine.fireEvent(CommonState.NEW, CommonEvent.MOUNTED, ctx);
        return CommonState.IDLE.getCode() == state.getCode();
    }

    /**
     * 下架路由器
     * @param shutdownRouterCmd 上架路由器
     * @return NEXT STATE
     */
    public Boolean shutdownRouter(ShutdownRouterUpdateCmd shutdownRouterCmd){
        StateMachine<CommonState, CommonEvent, CommonCtx> stateMachine = StateMachineFactory.get(StateMachineIdContent.common_state_machine_id);
        CommonCtx ctx = CommonCtx.builder()
                .id(shutdownRouterCmd.getId())
                .typeEnum(ResourceTypeEnum.ROUTER)
                .operator(shutdownRouterCmd.getOperator())
                .build();
        final CommonState state = stateMachine.fireEvent(CommonState.IDLE, CommonEvent.UNMOUNTED,ctx);
        return CommonState.IDLE.getCode() == state.getCode();
    }

    /**
     * 根据ID获取Router
     *
     * @param id Router ID
     * @return Router业务对象
     */
    public RouterCO getRouterById(Long id) {
        return routerCmdExe.queryById(id);
    }

    /**
     * 列表查询Router
     *
     * @param listQry 列表查询条件
     * @return Router业务对象列表
     */
    public List<RouterCO> routerListQuery(MetadataIdListQry listQry) {
        return routerCmdExe.listQuery(listQry);
    }

    /**
     * 分页查询Router
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括Router列表和分页信息
     */
    public PageResponse<RouterCO> routerPageQuery(RouterListByPageQry pageQry) {
        final Page<RouterDO> pageR = routerCmdExe.pageQuery(pageQry);
        final List<RouterDO> records = pageR.getRecords();
        final List<RouterCO> coList = GenericConvertor.toCOList(records,RouterCO.class);
        return PageResponse.of(coList,pageR.getTotal(),pageR.getSize(),pageR.getCurrent());
    }
}